package com.erp.warehouse.clouderp_warehouse.controller;

import com.erp.warehouse.clouderp_warehouse.pojo.merge.GoodsMergeCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.merge.GoodsMergeModify;
import com.erp.warehouse.clouderp_warehouse.service.merge.MergeService;
import com.erp.warehouse.clouderp_warehouse.util.UniqueOrderCode;
import org.comple.erp.pojo.ResultStatus;
import org.comple.erp.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 2025/11/13 11:41
 * author: 向远洋
 */
@RestController
@RequestMapping("/merge")
public class MergeController {
    @Autowired
    MergeService mergeService;
    @Autowired
    UniqueOrderCode uniqueOrderCode;

    @Value("${auth.staffId}")
    String staffId;
    @Value("${auth.companyId}")
    String companyId;

    @PostMapping("/page")
    public AjaxResult getPage(@RequestBody Map<String, Object> params) {
        params.put("staffId", staffId);
        params.put("companyId", companyId);
        return AjaxResult.back(ResultStatus.SUCCESS, "获取组装单列表成功！")
                .add("pageInfo", mergeService.getPageInfo(params));
    }

    @PostMapping("/create")
    public AjaxResult createGoodsMergeWithDetail(@RequestBody GoodsMergeCreate request) throws InterruptedException {
        //从jwt获取到用户，和相关公司code + staffName等
        //获取到当前订单编号
        String companyCode = "CNY001";
        String code = "ZZ" + uniqueOrderCode.getCode(companyCode, "ALLOT");
        request.getGoodsMerge().setCode(code);
        request.getGoodsMerge().setStaffId(staffId);
        request.getGoodsMerge().setStaffName("xxx员工");
        request.getGoodsMerge().setCompanyId(companyId);
        mergeService.createWithItems(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "组装单创建成功").add("inboundOrderRequest", request);
    }


    @PostMapping("/get/{id}")
    public AjaxResult getMergeWithDetail(@PathVariable String id) {
        return AjaxResult.back(ResultStatus.SUCCESS, "获取组装单和组装明细成功！")
                .add("mergeRequest", mergeService.getMergeWithDetail(id));
    }

    @PostMapping("/modify")
    public AjaxResult modifyMergeWithDetail(@RequestBody GoodsMergeModify request) {
        mergeService.modifyMergeWithDetail(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "修改组装单和组装明细成功！");
    }

    @PostMapping("/auditor")
    public AjaxResult auditorMergeWithDetail(@RequestBody GoodsMergeCreate request) {
        mergeService.auditorMergeWithDetail(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "审核组装单和组装明细成功！");
    }
}
